﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADNLib;

namespace ADNActions
{
	public static class Order
	{

		public static void MoveUp(List<XObject> items, List<XObject> moveTarget)
		{
			if (items != null && moveTarget != null)
			{
				for(int i = 0; i < moveTarget.Count; i++)
				{
					int index = items.IndexOf(moveTarget[i]);
					if (index < items.Count - 1) //not the last item
					{
						XObject obj = items[index];
						items.RemoveAt(index);
						items.Insert(index + 1, obj);
					}
				}
			}
		}

		public static void MoveDown(List<XObject> items, List<XObject> moveTarget)
		{
			if (items != null && moveTarget != null)
			{
				for (int i = 0; i < moveTarget.Count; i++)
				{
					int index = items.IndexOf(moveTarget[i]);
					if (index > 0) //not the first item
					{
						XObject obj = items[index];
						items.RemoveAt(index);
						items.Insert(index - 1, obj);
					}
				}
			}
		}

		public static void MoveTop(List<XObject> items, List<XObject> moveTarget)
		{
			if (items != null && moveTarget != null)
			{
				for (int i = 0; i < moveTarget.Count; i++)
				{
					int index = items.IndexOf(moveTarget[i]);
					XObject obj = items[index];
					items.RemoveAt(index);
					items.Add(obj);	//added to the bottom
				}
			}
		}

		public static void MoveBottom(List<XObject> items, List<XObject> moveTarget)
		{
			if (items != null && moveTarget != null)
			{
				for (int i = 0; i < moveTarget.Count; i++)
				{
					int index = items.IndexOf(moveTarget[i]);
					XObject obj = items[index];
					items.RemoveAt(index);
					items.Insert(i, obj);	//insert at the position they are in the selection
				}
			}
		}

	}
}
